package com.mianyang.zhangsan.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.dto.SecVideoDto;
import com.mianyang.zhangsan.model.entity.SecVideo;
import com.mianyang.zhangsan.service.SecVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author jiangjr
 * @date 2023-01-17
 */
@RestController
@RequestMapping("/video")
public class SecVideoController extends BaseController {
    @Autowired
    private SecVideoService videoService;


    /**
     * 查看所有视频
     */
    @GetMapping("/all")
    public R allVideo(SecVideoDto cond,
                      @RequestParam(value = "current", defaultValue = "1") int current,
                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Page<SecVideo> videos = videoService.selectVideos(cond, current, pageSize);
        return success("videos", videos);
    }

    @GetMapping("/hit/{top}")
    public R moreHitVideo(@PathVariable("top") int top) {
        List<SecVideo> videos = videoService.selectVideosOrderBy("hit", top);
        return success("videos", videos);
    }

    @PostMapping("/upload")
    public R uploadVideo(@RequestParam(value = "videoFile", required = false) MultipartFile videoFile,
                         @RequestParam("uploadType") int uploadType,
                         SecVideo video) {
        videoService.uploadVideo(videoFile, video, uploadType);
        return success();
    }

    @DeleteMapping("/delete/{videoId}")
    public R deleteVideo(@PathVariable("videoId") Long videoId) {
        videoService.removeById(videoId);
        return success();
    }

    /**
     * 点击视频
     */
    @GetMapping("/hit/up/{videoId}")
    public R hitVideo(@PathVariable("videoId") Long videoId) {
        videoService.addHitNum(videoId);
        return success();
    }

    /**
     * 视频评分
     */
    @GetMapping("/score")
    public R appraiseVideo(@RequestParam(value = "videoId") Long videoId,
                           @RequestParam(value = "score") double score) {
        videoService.appraise(videoId, score);
        return success();
    }
}
